package tech.tongyu.bob.demo.redis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

//@RestController
//@RequestMapping("/api")
public class UserController {


    //@Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/login")
    public String login(@RequestParam String username) {
        // 模拟用户登录成功
        String key = "user:" + username;

        // 将用户名缓存到 Redis
        redisTemplate.opsForValue().set(key, username);

        return "User " + username + " logged in and cached to Redis.";
    }

    @GetMapping("/getUser")
    public String getUser(@RequestParam String username) {
        String key = "user:" + username;

        // 从 Redis 中获取用户名
        Object cachedUsername = redisTemplate.opsForValue().get(key);

        if (cachedUsername != null) {
            return "Cached username: " + cachedUsername;
        } else {
            return "No cache found for username: " + username;
        }
    }
}
